package cn.stylefeng.guns.modular.rest.pb;

import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.scheduleInfo.entity.ScheduleInfo;
import cn.stylefeng.guns.modular.scheduleInfo.model.params.ScheduleDetailInfoParam;
import cn.stylefeng.guns.modular.scheduleInfo.model.params.ScheduleInfoParam;
import cn.stylefeng.guns.modular.scheduleInfo.model.params.ScheduleInfoPlantParam;
import cn.stylefeng.guns.modular.scheduleInfo.model.result.ScheduleDetailInfoResult;
import cn.stylefeng.guns.modular.scheduleInfo.model.result.ScheduleDetailInfoResultNew;
import cn.stylefeng.guns.modular.scheduleInfo.model.result.ScheduleInfoResult;
import cn.stylefeng.guns.modular.scheduleInfo.service.ScheduleDetailInfoService;
import cn.stylefeng.guns.modular.scheduleInfo.service.ScheduleInfoService;
import cn.stylefeng.guns.modular.storage.model.params.StorageOutParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageOutResult;
import cn.stylefeng.guns.modular.storage.service.StorageOutService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.Duration;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 排班控制器
 * 主要用于排班管理
 * @author wp
 * @date 2025-03-22
 */
@Controller
@RequestMapping("/rest/pb")
public class pbController extends BaseController {

    @Autowired
    private UserAop userAop;
    @Autowired
    private DeptService deptService;

    @Autowired
    private ScheduleInfoService scheduleInfoService;
    @Autowired
    private ScheduleDetailInfoService scheduleDetailInfoService;
    /**
     * 排班管理列表
     * @param scheduleInfoParam
     * @return
     */
    @ResponseBody
    @RequestMapping("/pbList")
    public ResponseData list(ScheduleInfoParam scheduleInfoParam) {
        User user = userAop.getUser();
//        Long deliverId = user.getDeliverId();
//        storageOutParam.setDeliverId(deliverId);
//        storageOutParam.setOutOrderStatus("webStorageOut");
        Long deptId = user.getDeptId();
        Dept bydeptId = deptService.getBydeptId(deptId);
        if(bydeptId != null && !bydeptId.getSimpleName().equals( "门店排班项目")){
            scheduleInfoParam.setStoreName(bydeptId.getSimpleName());
        }
        List<ScheduleInfoResult> listBySpec = scheduleInfoService.findListBySpec(scheduleInfoParam);

        return ResponseData.success(listBySpec);
    }

    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ScheduleInfoParam scheduleInfoParam) {

        List<ScheduleInfoResult> listBySpec = this.scheduleInfoService.findListBySpec(scheduleInfoParam);
        if (listBySpec.size() > 0) {
            return ResponseData.error("该日期已存在排班信息，请勿重复添加！");
        }
        //预估时效
        String estimatedTimeFrame = scheduleInfoParam.getEstimatedTimeFrame();
        //预估营业额
        String estimatedTurnover = scheduleInfoParam.getEstimatedTurnover();

        int i = Integer.valueOf(estimatedTurnover) / Integer.valueOf(estimatedTimeFrame);

        scheduleInfoParam.setEstimatedWorkingHours(i+"");
        this.scheduleInfoService.add(scheduleInfoParam);
        return ResponseData.success();
    }


    /**
     *
     * @param scheduleInfoParam
     * @return
     */

    @RequestMapping("/addPlant")
    @ResponseBody
    public ResponseData addPlant(ScheduleInfoPlantParam scheduleInfoParam) {



        //唯一性校验
        ScheduleDetailInfoParam scheduleDetailInfoParam = new ScheduleDetailInfoParam();
        scheduleDetailInfoParam.setPlantDate(scheduleInfoParam.getPlantDate());
        scheduleDetailInfoParam.setEmpName(scheduleInfoParam.getEmpName());
        scheduleDetailInfoParam.setStoreName(scheduleInfoParam.getStoreName());


        List<ScheduleDetailInfoResult> scheduleDetailInfoResults = this.scheduleDetailInfoService.checkUnique(scheduleDetailInfoParam);
        if(scheduleDetailInfoResults.size() > 0){
            return ResponseData.error("该日期已排班，请勿重复排班！");
        }

        ScheduleDetailInfoParam param = new ScheduleDetailInfoParam();
        param.setStoreName(scheduleInfoParam.getStoreName());
        param.setPlantDate(scheduleInfoParam.getPlantDate());
        param.setEmpName(scheduleInfoParam.getEmpName());
        param.setPosition(scheduleInfoParam.getPosition());
//        String planTime = scheduleInfoParam.getPlanTime();
        String startTime = scheduleInfoParam.getStartTime();
        String endTime = scheduleInfoParam.getEndTime();

        List<String> strings = tranceDate(startTime, endTime);

        AtomicInteger actualHour = new AtomicInteger();
        strings.forEach(s -> {
            switch (s){
                case "08:00 - 08:30" :
                    param.setClock0830("1");
                    actualHour.getAndIncrement();
                    break;
                case "08:30 - 09:00" :
                    param.setClock0900("1");
                    actualHour.getAndIncrement();
                    break;
                case "09:00 - 09:30" :
                    param.setClock0930("1");
                    actualHour.getAndIncrement();
                    break;
                case "09:30 - 10:00" :
                    param.setClock1000("1");
                    actualHour.getAndIncrement();
                    break;
                case "10:00 - 10:30" :
                    param.setClock1030("1");
                    actualHour.getAndIncrement();
                    break;
                case "10:30 - 11:00" :
                    param.setClock1100("1");
                    actualHour.getAndIncrement();
                    break;
                case "11:00 - 11:30" :
                    param.setClock1130("1");
                    actualHour.getAndIncrement();
                    break;
                case "11:30 - 12:00" :
                    param.setClock1200("1");
                    actualHour.getAndIncrement();
                    break;
                case "12:00 - 12:30" :
                    param.setClock1230("1");
                    actualHour.getAndIncrement();
                    break;
                case "12:30 - 13:00" :
                    param.setClock1300("1");
                    actualHour.getAndIncrement();
                    break;
                case "13:00 - 13:30" :
                    param.setClock1330("1");
                    actualHour.getAndIncrement();
                    break;
                case "13:30 - 14:00" :
                    param.setClock1400("1");
                    actualHour.getAndIncrement();
                    break;
                case "14:00 - 14:30" :
                    param.setClock1430("1");
                    actualHour.getAndIncrement();
                    break;
                case "14:30 - 15:00" :
                    param.setClock1500("1");
                    actualHour.getAndIncrement();
                    break;
                case "15:00 - 15:30" :
                    param.setClock1530("1");
                    actualHour.getAndIncrement();
                    break;
                case "15:30 - 16:00" :
                    param.setClock1600("1");
                    actualHour.getAndIncrement();
                    break;
                case "16:00 - 16:30" :
                    param.setClock1630("1");
                    actualHour.getAndIncrement();
                    break;
                case "16:30 - 17:00" :
                    param.setClock1700("1");
                    actualHour.getAndIncrement();
                    break;
                case "17:00 - 17:30" :
                    param.setClock1730("1");
                    actualHour.getAndIncrement();
                    break;
                case "17:30 - 18:00" :
                    param.setClock1800("1");
                    actualHour.getAndIncrement();
                    break;
                case "18:00 - 18:30" :
                    param.setClock1830("1");
                    actualHour.getAndIncrement();
                    break;
                case "18:30 - 19:00" :
                    param.setClock1900("1");
                    actualHour.getAndIncrement();
                    break;
                case "19:00 - 19:30" :
                    param.setClock1930("1");
                    actualHour.getAndIncrement();
                    break;
                case "19:30 - 20:00" :
                    param.setClock2000("1");
                    actualHour.getAndIncrement();
                    break;
                case "20:00 - 20:30" :
                    param.setClock2030("1");
                    actualHour.getAndIncrement();
                    break;
                case "20:30 - 21:00" :
                    param.setClock2100("1");
                    actualHour.getAndIncrement();
                    break;
                case "21:00 - 21:30" :
                    param.setClock2130("1");
                    actualHour.getAndIncrement();
                    break;
                case "21:30 - 22:00" :
                    param.setClock2200("1");
                    actualHour.getAndIncrement();
                    break;
                case "22:00 - 22:30" :
                    param.setClock2230("1");
                    actualHour.getAndIncrement();
                    break;

                case "22:30 - 23:59" :
                    param.setClock2400("1");
                    actualHour.getAndIncrement();
                    break;
            }
        });
        param.setActualHour(actualHour.get()/2+"");
//        ScheduleDetailInfoResult detail = this.scheduleDetailInfoService.findListBySpec(param);
//        List<ScheduleDetailInfoResult> detail = this.scheduleDetailInfoService.findListBySpec(param);
//        if(detail.size() > 0){
//            return ResponseData.error("该日期已排班，请勿重复排班！");
//        }


        this.scheduleDetailInfoService.add(param);

        //更新主表
//        根据门店、排班日期获取总表数据
        ScheduleInfoParam scheduleInfoParams = new ScheduleInfoParam();
        scheduleInfoParams.setStoreName(scheduleInfoParam.getStoreName());
        scheduleInfoParams.setPlantDate(scheduleInfoParam.getPlantDate());

        List<ScheduleInfoResult> listBySpec = this.scheduleInfoService.findListBySpec(scheduleInfoParams);
        ScheduleInfoResult scheduleInfoResult = listBySpec.get(0);
        Integer id = scheduleInfoResult.getId();
        String actualHour1 = scheduleInfoResult.getActualHour();
        ScheduleInfo scheduleInfo = new ScheduleInfo();
        scheduleInfo.setId(id);
        scheduleInfo.setActualHour(Integer.valueOf(param.getActualHour())+Integer.valueOf(actualHour1) +"");
        this.scheduleInfoService.updateById(scheduleInfo);
        //        this.scheduleInfoService.add(scheduleInfoParam);
        return ResponseData.success();
    }

    @RequestMapping("/plantDetail")
    @ResponseBody
    public ResponseData plantDetail(ScheduleDetailInfoParam param) {

        ScheduleInfo scheduleInfo = this.scheduleInfoService.getById(param.getId());
        ScheduleDetailInfoParam detailParam = new ScheduleDetailInfoParam();
        detailParam.setStoreName(scheduleInfo.getStoreName());
        detailParam.setPlantDate(scheduleInfo.getPlantDate());

        List<ScheduleDetailInfoResult> detail = this.scheduleDetailInfoService.findListBySpec(detailParam);
        return ResponseData.success(0,"成功",detail);
    }

    @RequestMapping("/plantDetail1")
    @ResponseBody
    public ResponseData plantDetail1(ScheduleDetailInfoParam param) {

        ScheduleInfo scheduleInfo = this.scheduleInfoService.getById(param.getId());
        ScheduleDetailInfoParam detailParam = new ScheduleDetailInfoParam();
        detailParam.setStoreName(scheduleInfo.getStoreName());
        detailParam.setPlantDate(scheduleInfo.getPlantDate());

        List<ScheduleDetailInfoResult> details = this.scheduleDetailInfoService.findListBySpec(detailParam);
        List<ScheduleDetailInfoResultNew>  detailNew = new ArrayList<>();
        for (ScheduleDetailInfoResult detail : details) {
            ScheduleDetailInfoResultNew  scheduleDetailInfoResultNew = new  ScheduleDetailInfoResultNew();

            scheduleDetailInfoResultNew.setEmpName(detail.getEmpName());
            scheduleDetailInfoResultNew.setPlantDate(detail.getPlantDate());
            scheduleDetailInfoResultNew.setStoreName(detail.getStoreName());
            scheduleDetailInfoResultNew.setPosition(detail.getPosition());

            List<Map<String,String>> scheduleDetailInfo = new ArrayList<>();
            Map<String,String> map = new HashMap<>();
            map.put("key","0830");
            map.put("value",detail.getClock0830());
            Map<String,String> map1 = new HashMap<>();
            map1.put("key","0900");
            map1.put("value",detail.getClock0900());
            Map<String,String> map2 = new HashMap<>();
            map2.put("key","0930");
            map2.put("value",detail.getClock0930());
            Map<String,String> map3 = new HashMap<>();
            map3.put("key","1000");
            map3.put("value",detail.getClock1000());
            Map<String,String> map4 = new HashMap<>();
            map4.put("key","1030");
            map4.put("value",detail.getClock1030());
            Map<String,String> map5 = new HashMap<>();
            map5.put("key","1100");
            map5.put("value",detail.getClock1100());
            Map<String,String> map6 = new HashMap<>();
            map6.put("key","1130");
            map6.put("value",detail.getClock1130());
            Map<String,String> map7 = new HashMap<>();
            map7.put("key","1200");
            map7.put("value",detail.getClock1200());
            Map<String,String> map8 = new HashMap<>();
            map8.put("key","1230");
            map8.put("value",detail.getClock1230());
            Map<String,String> map9 = new HashMap<>();
            map9.put("key","1300");
            map9.put("value",detail.getClock1300());
            Map<String,String> map10 = new HashMap<>();
            map10.put("key","1330");
            map10.put("value",detail.getClock1330());
            Map<String,String> map11 = new HashMap<>();
            map11.put("key","1400");
            map11.put("value",detail.getClock1400());
            Map<String,String> map12 = new HashMap<>();
            map12.put("key","1430");
            map12.put("value",detail.getClock1430());
            Map<String,String> map13 = new HashMap<>();
            map13.put("key","1500");
            map13.put("value",detail.getClock1500());
            Map<String,String> map14 = new HashMap<>();
            map14.put("key","1530");
            map14.put("value",detail.getClock1530());
            Map<String,String> map15 = new HashMap<>();
            map15.put("key","1600");
            map15.put("value",detail.getClock1600());
            Map<String,String> map16 = new HashMap<>();
            map16.put("key","1630");
            map16.put("value",detail.getClock1630());
            Map<String,String> map17 = new HashMap<>();
            map17.put("key","1700");
            map17.put("value",detail.getClock1700());
            Map<String,String> map18 = new HashMap<>();
            map18.put("key","1730");
            map18.put("value",detail.getClock1730());
            Map<String,String> map19 = new HashMap<>();
            map19.put("key","1800");
            map19.put("value",detail.getClock1800());
            Map<String,String> map20 = new HashMap<>();
            map20.put("key","1830");
            map20.put("value",detail.getClock1830());
            Map<String,String> map21 = new HashMap<>();
            map21.put("key","1900");
            map21.put("value",detail.getClock1900());
            Map<String,String> map22 = new HashMap<>();
            map22.put("key","1930");
            map22.put("value",detail.getClock1930());
            Map<String,String> map23 = new HashMap<>();
            map23.put("key","2000");
            map23.put("value",detail.getClock2000());
            Map<String,String> map24 = new HashMap<>();
            map24.put("key","2030");
            map24.put("value",detail.getClock2030());
            Map<String,String> map25 = new HashMap<>();
            map25.put("key","2100");
            map25.put("value",detail.getClock2100());
            Map<String,String> map26 = new HashMap<>();
            map26.put("key","2130");
            map26.put("value",detail.getClock2130());
            Map<String,String> map27 = new HashMap<>();
            map27.put("key","2200");
            map27.put("value",detail.getClock2200());
            Map<String,String> map28 = new HashMap<>();
            map28.put("key","2230");
            map28.put("value",detail.getClock2230());
            Map<String,String> map29 = new HashMap<>();
            map29.put("key","2400");
            map29.put("value",detail.getClock2400());


            scheduleDetailInfo.add(map);
            scheduleDetailInfo.add(map1 );
            scheduleDetailInfo.add(map2 );
            scheduleDetailInfo.add(map3 );
            scheduleDetailInfo.add(map4 );
            scheduleDetailInfo.add(map5 );
            scheduleDetailInfo.add(map6 );
            scheduleDetailInfo.add(map7 );
            scheduleDetailInfo.add(map8 );
            scheduleDetailInfo.add(map9 );
            scheduleDetailInfo.add(map10);
            scheduleDetailInfo.add(map11);
            scheduleDetailInfo.add(map12);
            scheduleDetailInfo.add(map13);
            scheduleDetailInfo.add(map14);
            scheduleDetailInfo.add(map15);
            scheduleDetailInfo.add(map16);
            scheduleDetailInfo.add(map17);
            scheduleDetailInfo.add(map18);
            scheduleDetailInfo.add(map19);
            scheduleDetailInfo.add(map20);
            scheduleDetailInfo.add(map21);
            scheduleDetailInfo.add(map22);
            scheduleDetailInfo.add(map23);
            scheduleDetailInfo.add(map24);
            scheduleDetailInfo.add(map25);
            scheduleDetailInfo.add(map26);
            scheduleDetailInfo.add(map27);
            scheduleDetailInfo.add(map28);
            scheduleDetailInfo.add(map29);

            scheduleDetailInfoResultNew.setScheduleDetailInfo(scheduleDetailInfo);
            detailNew.add(scheduleDetailInfoResultNew);
        }


        return ResponseData.success(0,"成功",detailNew);
    }


    List<String> tranceDate(String startDate,String endDate){
        // 定义时间范围
        LocalTime startTime = LocalTime.parse(startDate.trim());
        LocalTime endTime = LocalTime.parse(endDate.trim());
        LocalTime endTime1 = LocalTime.parse("00:30:00");
        // 每半小时分割
        Duration interval = Duration.ofMinutes(30);
        List<String> timeRanges = new ArrayList<>();
        List<String> timeRanges1 = new ArrayList<>();

        // 循环生成时间范围
        LocalTime currentTime = startTime;
        while (currentTime.isBefore(endTime)) {
            LocalTime nextTime = currentTime.plus(interval);
            if (nextTime.isAfter(endTime)) {
                nextTime = endTime; // 确保不超过结束时间
            }
            timeRanges.add(currentTime.toString() + " - " + nextTime.toString());
            timeRanges1.add(nextTime.toString().replace(":",""));
            currentTime = nextTime;
            if(currentTime.isBefore(endTime1)){
                break;
            }
        }
        System.out.println("timeRanges = " + timeRanges);
        System.out.println("timeRanges = " + timeRanges1);
        // 输出结果
        timeRanges.forEach(System.out::println);
        return timeRanges;
    }
}
